﻿using CMS.Controls;
using CMS.Controls.DisplayTabPages;
using CMS.Controls.Messages;
using CMS.Dialogs;
using CMS.REST;
using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CMS.Forms
{
    public partial class MainForm : Form, IMessageDestination
    {
        public MainForm()
        {
            InitializeComponent();
            _InitTabs();
        }



        /// <summary>
        /// 初始化所有选项卡
        /// </summary>
        private void _InitTabs()
        {
            TC_Main.TabPages.AddRange(new TabPage[]
            {
                new LazyTabPage(()=>new CourseTabPage()){Text="实验室课表"},
                new LazyTabPage(()=>new DutyTabPage()){Text="值班安排表"},
                new LazyTabPage(()=>new ChartTabPage()){Text="报表导出"},
                new LazyTabPage(()=>new SettingTabPage()){Text="数据调整"},
            });


        }

        #region 主界面菜单响应函数

        private void TSMI_DataImport_Click(object sender, EventArgs e)
        {
            if (Permission.CheckPermission(Permission.ImportData))
            {
                new ImportExcelFileDialog().ShowDialog(this);
            }
            else
            {
                MessageBox.Show(this, "很抱歉,您没有权限执行此类操作", "权限不足");
            }
        }

        private void TSMI_DataExport_Click(object sender, EventArgs e)
        {
            if (Permission.CheckPermission(Permission.ExportData))
            {
                new ExportExcelFileDialog().ShowDialog(this);
            }
            else
            {
                MessageBox.Show(this, "很抱歉,您没有权限执行此类操作", "权限不足");
            }
        }

        private void TSMI_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        public void ReceiveMessage(MessageSentEventArgs args)
        {
            Action action = new Action(() =>
             {
                 switch (args.MsgType)
                 {
                     case MsgType.OK:
                         TSSL_Status.ForeColor = Color.Green;
                         TSSL_Status.BackColor = SystemColors.Control;
                         break;
                     case MsgType.WARN:
                         TSSL_Status.ForeColor = Color.YellowGreen;
                         TSSL_Status.BackColor = SystemColors.Control;
                         break;
                     case MsgType.ERROR:
                         TSSL_Status.ForeColor = Color.Red;
                         TSSL_Status.BackColor = SystemColors.Control;
                         break;
                     case MsgType.FATAL:
                         TSSL_Status.ForeColor = Color.White;
                         TSSL_Status.BackColor = Color.Red;
                         break;
                     default:
                         throw new NotImplementedException();
                 }
                 TSSL_Status.Text = args.Message;
             });
            if (InvokeRequired)
            {
                Invoke(action);
            }
            else
            {
                action();
            }

        }

        private void TSMI_AboutThis_Click(object sender, EventArgs e)
        {
            new AboutDialog().ShowDialog();
        }

        private void TSMI_changePassword_Click(object sender, EventArgs e)
        {
            new ChangePasswordDialog().ShowDialog();
        }

        private void TSMI_Logout_Click(object sender, EventArgs e)
        {
            Hide();
            RESTClient.HttpPost("apis/v1/logout", new JObject());
            if (LoginDialog.GetInstance().ShowDialog() == DialogResult.OK)
            {
                Show();
            }
            else
            {
                Close();
            }
        }
    }
}
